﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Web.Security;
using CCF20Model;
using System.Text;
using System.Collections.Generic;

namespace CCF20
{
    /// <summary>
    /// Interaction logic for Login.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        private Storyboard closeStoryBoard;
        public LoginWindow()
        {
            InitializeComponent();
            closeStoryBoard = (Storyboard)FindResource("closeStoryBoard");
            System.Reflection.Assembly.LoadFrom("WiseLinkWPF.Windows.Controls.GridView.dll");
            System.Reflection.Assembly.LoadFrom("WiseLinkWPF.Windows.Controls.Navigation.dll");
            System.Reflection.Assembly.LoadFrom("WiseLinkWPF.Windows.Controls.dll");
            System.Reflection.Assembly.LoadFrom("WiseLinkWPF.Windows.Data.dll");
            System.Reflection.Assembly.LoadFrom("WiseLinkWPF.Windows.Controls.Input.dll");
            System.Reflection.Assembly.LoadFrom("WiseLinkWPF.Windows.Controls.Gauge.dll");
            System.Reflection.Assembly.LoadFrom("WiseLinkWPF.Windows.Controls.DataVisualization.dll");
            
        }
        private bool closeStoryBoardCompleted = false;
        
        private void Window_Closing(object sender, CancelEventArgs e) 
        {     
            if (!closeStoryBoardCompleted)    
            {         
                closeStoryBoard.Begin(this);   
                //e.Cancel = true;   
            }
        }
        private void closeStoryBoard_Completed(object sender, EventArgs e)
        {
            closeStoryBoardCompleted = true;
            this.Close();
        }
        private void RadButton_Click(object sender, RoutedEventArgs e)
        {
            string action = ((Button)sender).Tag.ToString();
            string userName = UserName.Text.Trim();
            string passWord = UserPassword.Text.Trim();
            bool isValidUser = false;
            if (action == "Login")
            {
                if (userName.Equals("Admin",StringComparison.OrdinalIgnoreCase)
                    && passWord.Equals("Admin",StringComparison.OrdinalIgnoreCase))
                {
                    isValidUser = true;
                    

                }
                else if( Membership.ValidateUser(userName,passWord) )
                {
                    StringBuilder sb = new StringBuilder();
                    PermissionModel permissionModel = new PermissionModel();
                    //IList<string> permissionList = new List<string>();
                    foreach (string role in Roles.GetRolesForUser(userName))
                    {
                        
                        Application.Current.Properties["AllowedPermission"] += permissionModel.GetPermissionForRole(role);
                    }
                    isValidUser = true;
                }
                
                if ( isValidUser )
                {
                    Application.Current.Properties["CurrentUser"] = userName;
                    MainWindow mw = new MainWindow();
                    ZICMonitorModel.UpdateZICUrl(App.ZICUrl, App.IsJava);
                    this.Dispatcher.BeginInvoke(new Action(() => mw.Show()));
                    this.Dispatcher.BeginInvoke(new Action(() => LaunchZICMonitor()));
                    Window_Closing(null, null);
                }
            }else if (action == "Cancel")
            {
                //MainWindow.LoginSuccess = false;
                
                this.Close();
            }
            else if (action == "Configuration")
            {
                //MainWindow.LoginSuccess = false;

                Configuration config = new Configuration();
                config.ShowDialog();
            }
        }
        private void RemoveOldLogFiles()
        {
            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;

            string myDir = path.Remove(path.LastIndexOf("\\"));
            DirectoryInfo dirInfo = new DirectoryInfo(myDir);
            FileInfo[] files = dirInfo.GetFiles("*.log");
            foreach (FileInfo file in files)
            {
                if ((DateTime.Now - file.LastAccessTime) > new TimeSpan(7, 0, 0, 0))
                    file.Delete();
            }
        }
            
         
        private void LaunchZICMonitor()
        {
            Process[] runningProcs = Process.GetProcesses(".");
            foreach (Process p in runningProcs)
            {
                if (p.ProcessName.Contains("ZICMonitor"))
                {
                    p.Kill();
                }
            }

            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string myDir = path.Remove(path.LastIndexOf("\\"));
            DirectoryInfo dirInfo = new DirectoryInfo(myDir);
            FileInfo[] files = dirInfo.GetFiles("ZICMonitor.exe");
            if (files.GetLength(0)>0)
                Process.Start("ZICMonitor.exe");
        }
    }
}
